var model = require('../../../model/model');
var logger = require('pomelo-logger').getLogger('playerHandler',__filename);  //日志文件
module.exports = function(app)
{
    return new Handler(app);
};

var Handler = function(app)
{
    this.app = app;
    this.channelService = this.app.get('channelService');
};
var handler = Handler.prototype;

handler.playerInfoGet = function(msg ,session ,next)
{
    var playerId = msg.playerId;
    model.getPlayerInfo(playerId,function(err,data)
    {
        if( err )
        {
            logger.error(err);
        }
        else
        {
            var res = 
            {
                'playerId':data.playerId,
                'nickName':data.nickName,
                'coinNum':data.coinNum,
                'winNum':data.winNum
            }
            next(null,res);
        }
    });
};

handler.nickNameUpdate = function(msg ,session ,next)
{
    var uid = session.uid;
    var nickName = msg.nickName; //获取需要修改的昵称
    if( nickName )
    {
        var update = {"nickName":nickName};
        model.syncPlayer(uid,update);
    }
    next();
};

handler.scoreListGet = function(msg,session,next)
{
    model.getScoreList(function(err,scoreList)
    {
        if( err )
        {
            logger.error(err);
        }
        else
        {
            next(null,{'scoreList':scoreList});
        }
    });
};

handler.actorBuy  = function(msg ,session ,next)
{
    var playerId = session.uid;
    var actorId = msg.actorId;
    model.actorBuy(actorId,actorId);
    next();
};

handler.propUse = function(msg ,session ,next)
{
    var playerId = session.uid;
    var propId = msg.propId;
    model.propUse(playerId,propId);
    next();
};

handler.skillValueGet = function(msg ,session ,next)
{
    model.getAllSkillValue(function(err,res)
    {
        next(null,res);
    });
};

handler.cardListGet = function(msg ,session ,next)
{
    var playerId = session.uid;
    model.createCardList(playerId,function(err,res)
    {
        next(null,res);
    });
};

handler.goodsBuy = function(msg,session,next)
{
    var playerId = session.uid;
    var goodsId = msg.goodsId;
    model.goodsBuy(playerId,goodsId);
    next();
};

handler.shopInfoGet = function(msg,session,next)
{
    model.getShopInfo(function(err,res)
    {
        next(null,res);
    });
};

handler.dayTaskListGet = function(msg ,session , next)
{
    model.getDayTaskList(function(err,res)
    {
        if( err )
        {
            logger.error(err);
        }
        else
        {
            next(null,res);
        }
    });
};

handler.playerEran = function(msg , session , next)
{
    var playerId  = session.uid;
    var coinNum = msg.coinNum;
    model.playerEran(playerId,coinNum);
    next();
};

handler.propBuy = function(msg , session , next)
{
    var playerId = session.uid;
    var coinNum = msg.coinNum;
    var props = msg.props;
    model.propBuy(playerId,props,coinNum);
    next();
};

handler.powerBuy = function(msg ,session ,next)
{
    var playerId  = session.uid;
    var powerId= msg.id ; //体力Id
    model.powerBuy(playerId,powerId);
    next();
};

handler.secondOpen = function(msg,session,next)
{
    var playerId = session.uid;
    var actorId = msg.actorId;
    model.secondOpen(playerId,actorId) ;
    next();
};

handler.powerSync = function(msg,session,next)
{
    var playerId = session.uid;
    var power = msg.power;
    var update = {"power":power};
    model.syncPlayer(playerId,update);
    next();
};

handler.playerInfoSync = function(msg ,session ,next)
{
    var playerId = session.uid;
    model.playerInfoSync(playerId,msg);
    next();
};

handler.expSync = function(msg,session ,next)
{
    var playerId = session.uid;
    var actorId = msg.actorId;
    var exp = msg.exp;
    model.expSync(playerId,actorId,exp);
    next();
};

handler.guideSync = function(msg,session ,next)
{
    var uid = session.uid;
    var guide = msg.guide;
    var update = {"guide":guide};
    model.syncPlayer(uid,update);
    next();
};

handler.diamondSync = function(msg,session ,next)
{
    var uid = session.uid;
    var diamondNum = msg.diamondNum;
    var update = {"diamondNum":diamondNum};
    model.syncPlayer(uid,update);
    next();
};